from bokeh.palettes import Spectral5
from bokeh.sampledata.autompg import autompg as df
from bokeh.transform import factor_cmap
from bokeh.sampledata.autompg import autompg_clean as df
from bokeh.plotting import figure
from pyecharts import options as opts
from pyecharts.charts import Geo
import json
import pandas as pd
import numpy as np

def mpg():
    '''
    Manufacturer grouped by # Cylinders
    '''
    df.cyl = df.cyl.astype(str)
    # df.yr = df.yr.astype(str)
    group = df.groupby(['cyl', 'mfr'])  # 复合条件分组，[缸数、厂家]
    index_cmap = factor_cmap('cyl_mfr', palette=Spectral5, factors=sorted(df.cyl.unique()), end=1)
    # 画布
    p = figure(plot_width=1000, plot_height=500, title="Mean MPG by # Cylinders and Manufacturer",
               x_range=group, tooltips=[("MPG", "@mpg_mean"), ("Cyl, Mfr", "@cyl_mfr")])
    # 绘图
    p.vbar(x='cyl_mfr', top='mpg_mean', width=1, source=group,
           line_color="white", fill_color=index_cmap, )  # 尾气排放量均值
    # 其他
    p.y_range.start = 0
    p.x_range.range_padding = 0.05  # 同css中的padding
    p.xgrid.grid_line_color = None
    p.xaxis.axis_label = "Manufacturer grouped by # Cylinders"
    p.xaxis.major_label_orientation = 1.2  # x轴标签旋转
    p.outline_line_color = None

    return p

def vbar_demo():
    p = figure(plot_width=300, plot_height=300)
    p.vbar(
        x=[1, 2, 3, 4],
        width=0.5,
        bottom=0,
        top=[1.7, 2.2, 4.6, 3.9],
        color='navy'
    )
    return p

def gaoxiaoshuju():
    with open('gaoxiao.json','r',encoding='utf-8') as f:
        data=json.load(f)
        shen=[[i['province_name'],len(j['universities'])]for i in data['schools'] for j in i['cities']]
        df=pd.DataFrame(shen)
        biao=df.groupby(0).sum()
        biao.index.name = '市'
        biao.columns=["数量"]
        shi=biao.index.tolist()
        shi1=['广西' if i =='广西壮族自治区' else i for i in shi]
        shi2=['内蒙古' if i =='内蒙古自治区' else i for i in shi1]
        shi3=['西藏' if i =='西藏自治区' else i for i in shi2]
        shi4=['宁夏' if i =='宁夏回族自治区' else i for i in shi3]
        shi5=['新疆' if i =='新疆维吾尔自治区' else i for i in shi4]
        data_shi=biao.values.tolist()
        shuliang=[]
        for i in data_shi:
            shuliang.append(i[0])
        zipped = zip(shi5,shuliang)
        shuju=[list(z) for z in zipped]  
        c = (
            Geo()
            .add_schema(maptype="china")
            .add("geo",shuju)
            .set_series_opts(
                label_opts=opts.LabelOpts(
                    is_show=False)
            )
            .set_global_opts(
                visualmap_opts=opts.VisualMapOpts(
                max_=30,
                ), 
                title_opts=opts.TitleOpts(
                    title="Geo-基本示例"
                )
            )
        #     .render("geo_base.html")
        )
        return c.render()

def ditu():
    c = (
        Geo()
            .add_schema(maptype="广东")
            .add(
                "geo",
                [list(z) for z in zip(Faker.guangdong_city, Faker.values())],
                type_=ChartType.HEATMAP,
            )
            .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
            .set_global_opts(
                visualmap_opts=opts.VisualMapOpts(), title_opts=opts.TitleOpts(title="Geo-广东地图")
            )
            
        )
    return c
    
    
